﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using Manager;
using Model;
using System.Data;
using Manager.CustomerHelper;
namespace HealthSystem
{
    public partial class ChooseDistrict : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitMenu();
            }
        }

        private void BindSheng()
        {
            //ddlSheng.DataSource = SHENG_JSON;
            //ddlSheng.DataBind();

            //ddlSheng.Items.Insert(0, new ListItem("选择省份", "-1"));
        }

        private void InitMenu()
        {

            List<CustomDistrict> menus = new List<CustomDistrict>();
            //treeMenu.Nodes.Clear();
            // 当前角色可以显示的菜单列表
         
            HW_DistrictManager manager = new HW_DistrictManager();
            //DataTable list= manager.GetSubList(CustomerProfile.DistrictId);
            //CustomMenu menu = null;
            //for (int i = 0; i < list.Rows.Count; i++)
            //{
            //    menu = new CustomMenu();
            //    menu.Id =Convert.ToInt32(list.Rows[i]["DistrictCode"]);
            //    menu.Name = list.Rows[i]["NAME"] as string;
            //    menu.ParentId = list.Rows[i]["ParentDistrictCode"] is DBNull ? 44 : Convert.ToInt32(list.Rows[i]["ParentDistrictCode"]);
            //    menus.Add(menu);
            //}
            DistrictTree districtTree = new DistrictTree();
            menus = districtTree.GetDistrictTree(CustomerProfile.DistrictId);
            // 生成树
            ResolveMenuTree(menus,CustomerProfile.DistrictId, treeMenu.Nodes);

            if (treeMenu.Nodes.Count == 0)
            {
                Response.Write("");
                Response.End();

                return;
            }

            // 展开第一个树节点
            //treeMenu.Nodes[0].Expanded = true;

            #region old code
            //if (XConfigHelper.MenuType.ToLower() == "accordion")
            //{
            //    Accordion accordion = new Accordion();
            //    regionLeft.Items.Add(accordion);
            //    accordion.ShowBorder = false;
            //    accordion.ShowHeader = false;

            //    foreach (MyMenu menu in newMenus)
            //    {
            //        if (menu.ParentMenuId == 0)
            //        {
            //            AccordionPane pane = new AccordionPane();
            //            accordion.Panes.Add(pane);
            //            pane.Title = menu.Name;
            //            pane.ShowBorder = false;

            //            Tree tree = new Tree();
            //            pane.Items.Add(tree);
            //            tree.ShowBorder = false;
            //            tree.ShowHeader = false;
            //            tree.AutoScroll = true;
            //            tree.EnableArrows = true;

            //            ResolveMenuTree(newMenus, menu.Id, tree.Nodes);
            //        }
            //    }
            //}
            //else
            //{
            //    Tree tree = new Tree();
            //    regionLeft.Items.Add(tree);
            //    tree.ShowBorder = false;
            //    tree.ShowHeader = false;
            //    tree.AutoScroll = true;
            //    tree.EnableArrows = true;

            //    ResolveMenuTree(newMenus, 0, tree.Nodes);
            //} 
            #endregion
        }

        /// <summary>
        /// 生成菜单树
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentMenuId"></param>
        /// <param name="nodes"></param>
        private void ResolveMenuTree(List<CustomDistrict> menus, string parentMenuId, ExtAspNet.TreeNodeCollection nodes)
        {
            foreach (CustomDistrict menu in menus)
            {
                if (menu.ParentId == parentMenuId)
                {
                    ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();


                    node.Text = menu.Name;
                    node.NodeID = menu.Id;
                    node.Icon = Icon.UserHome;
                    //node.IconUrl = menu.ImageUrl;
                    //if (!String.IsNullOrEmpty(menu.NavigateUrl))
                    //{
                    //    node.EnablePostBack = true;
                    //    //node.EnablePostBack = true;
                    //    //node.AutoPostBack = true;
                    //    node.Target = ResolveUrl(menu.NavigateUrl);
                    //    //PageContext.RegisterStartupScript(mainTabStrip.GetAddTabReference(node.NodeID, node.NavigateUrl, node.Text, IconHelper.GetIconUrl(Icon.HouseConnect), true));
                    //    //node.OnClientClick = String.Format("addTab('{0}','{1}','{2}')", node.NodeID, ResolveUrl(menu.NavigateUrl), node.Text.Replace("'", ""));
                    //}
                    if (menu.IsTreeLeaf)
                    {
                        node.Icon = Icon.UserMature;
                        node.Leaf = true;
                    }
                    node.EnablePostBack = true;
                    node.Expanded = false;
                    nodes.Add(node);
                    //node.EnablePostBack = true;
                    ResolveMenuTree(menus, menu.Id, node.Nodes);
                }
            }
        }
        protected void TreeSelect(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
            //e.Node.
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(e.Node.NodeID,e.Node.Text) + ActiveWindow.GetHidePostBackReference());
        }
    }
}